package com.mate.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mate.constant.MovieType;
import com.mate.model.*;
import com.mate.model.json.Coming;
import com.mate.model.vo.MovieDetailVo;
import com.mate.model.vo.MovieVo;
import com.mate.model.vo.RankVo;
import com.mate.service.*;
import com.mate.utils.MovieInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 电影详情 前端控制器
 * </p>
 *
 * @author boy
 * @since 2020-05-08
 */
@RestController
@RequestMapping("/mMovie")
public class MMovieController extends BaseController {

    @Resource
    private IMWishService wishService;

    @Resource
    private IMRankService rankService;

    @Resource
    private IMMovieService movieService;

    @Resource
    private IMCommentService commentService;

    @Resource
    private IMActorsNameService actorsNameService;

    @RequestMapping("/getComing")
    public String getComing() throws ParseException {
        List<Coming.ComingBean> list = MovieInfo.getComing();
        for (Coming.ComingBean s : list) {
            MMovie mMovie = movieService.selectById(s.getId());
            if (mMovie != null) {
                mMovie.setCreateTime(new Date());
            } else {
                mMovie = new MMovie();
                mMovie.setId(s.getId());
                mMovie.setTitle(s.getNm());
                mMovie.setCoverPic(s.getImg());
                mMovie.setComingTime(s.getRt());
                mMovie.setExpectedNumber(s.getWish());
                mMovie.setCoverPic(s.getImg().replaceAll("w.h", "128.180"));
                mMovie.setActorName(StringUtils.isNotBlank(s.getStar()) ? s.getStar() : "");
                mMovie.setCreateTime(new Date());
                mMovie.setType(MovieType.COMING.getCode());
            }
            movieService.insertOrUpdate(mMovie);
        }
        return "success";
    }

    @RequestMapping("/getAlready")
    public String getAlready() throws ParseException {
        List<MovieVo> list = MovieInfo.getAlready();
        for (MovieVo s : list) {
            MMovie mMovie = new MMovie();
            mMovie.setId(s.getId());
            mMovie.setCoverPic(s.getCoverPic());
            mMovie.setTitle(s.getTitle());
            mMovie.setEnglishTitle(s.getEnglishTitle());
            mMovie.setMovieType(s.getMovieType());
            mMovie.setComingTime(s.getComingTime());
            mMovie.setScore(s.getScore());
            mMovie.setCreateTime(new Date());
            mMovie.setType(MovieType.ALREADY.getCode());
            movieService.insertOrUpdate(mMovie);
        }
        return "success";
    }

    /**
     * 票房排行
     */
    @RequestMapping("/getRank")
    public String getRank() throws ParseException {
        rankService.delete(new EntityWrapper<>());
        List<RankVo.MoviesBean> list = MovieInfo.getRank();
        for (RankVo.MoviesBean s : list) {
            MRank rank = new MRank();
            rank.setMovieId(s.getId());
            rank.setImg(s.getImg());
            rank.setDir(s.getDir());
            rank.setNm(s.getNm());
            rank.setRt(s.getRt());
            rank.setSc(s.getSc());
            rank.setStar(s.getStar());
            rank.setPubDesc(s.getPubDesc());
            rank.setWish(s.getWish());
            rank.setSumBoxOffice(s.getSumBoxOffice());
            rank.setCreateTime(new Date());
            rankService.insertOrUpdate(rank);
        }
        return "success";
    }

    /**
     * 票房期待想看排行
     */
    @RequestMapping("/getRankClick")
    public String getRankClick() throws ParseException {
        wishService.delete(new EntityWrapper<>());
        List<RankVo.MoviesBean> list = MovieInfo.getRankClick();
        for (RankVo.MoviesBean s : list) {
            MWish wish = new MWish();
            wish.setMovieId(s.getId());
            wish.setImg(s.getImg());
            wish.setDir(s.getDir());
            wish.setNm(s.getNm());
            wish.setRt(s.getRt());
            wish.setSc(s.getSc());
            wish.setStar(s.getStar());
            wish.setPubDesc(s.getPubDesc());
            wish.setWish(s.getWish());
            wish.setSumBoxOffice(s.getSumBoxOffice());
            wish.setCreateTime(new Date());
            wishService.insertOrUpdate(wish);
        }
        return "success";
    }

    /**
     * 电影详情
     */
    @RequestMapping("/getDetail")
    public String getDetail() throws Exception {
        List<MMovie> list = movieService.selectList(new EntityWrapper<MMovie>().and("is_delete='0'"));
        for (MMovie s : list) {
            MovieDetailVo movieDetailVo = MovieInfo.getDetail(s.getId().toString());
            if (movieDetailVo != null) {
                s.setEnglishTitle(movieDetailVo.getMovie().getEnm());
                s.setMovieType(movieDetailVo.getMovie().getCat());
                s.setScreenType(movieDetailVo.getMovie().getMovieType() + "");
                s.setActorName(movieDetailVo.getMovie().getDir());
                s.setAddress(movieDetailVo.getMovie().getSrc());
                s.setComingTime(movieDetailVo.getMovie().getOnlineDate());
                s.setMovieTime(movieDetailVo.getMovie().getDur() == 0 ? "" : movieDetailVo.getMovie().getDur() + "");
                s.setExpectedNumber(movieDetailVo.getMovie().getWish());
                s.setInfo(movieDetailVo.getMovie().getDra());
                List<MovieDetailVo.CelebritiesBean> celebritiesBeanList = movieDetailVo.getCelebrities();
                if (celebritiesBeanList != null && celebritiesBeanList.size() != 0) {
                    if (actorsNameService.selectCount(new EntityWrapper<MActorsName>().and("movie_id = {0}", s.getId())) == 0) {
                        List<MActorsName> celebritiesVoList = new ArrayList<>();
                        for (int i = 0; i < celebritiesBeanList.size(); i++) {
                            MovieDetailVo.CelebritiesBean ss = celebritiesBeanList.get(i);
                            MActorsName actorsName = new MActorsName();
                            actorsName.setActorId(ss.getId());
                            actorsName.setMovieId(s.getId());
                            actorsName.setaName(ss.getCnm() + " " + ss.getDesc());
                            actorsName.setaPic(ss.getAvatar());
                            actorsName.setCreateTime(new Date());
                            actorsName.setIsDelete("0");
                            celebritiesVoList.add(actorsName);
                        }
                        if (celebritiesVoList.size() > 0) {
                            actorsNameService.insertBatch(celebritiesVoList);
                        }
                    }
                }
                s.setStills(JSON.toJSONString(movieDetailVo.getMovie().getPhotos()));
                if (movieDetailVo.getComments().getTotal() > 0) {
                    if (movieDetailVo.getMovie().getDra().equals("一天之内一群人围绕古玩发生的一连串啼笑皆非的故事。")) {
                        System.out.println(1);
                    }
                    List<MovieDetailVo.CommentsBeanX.HotCommentsBean> hotList = movieDetailVo.getComments().getHotComments();
                    for (MovieDetailVo.CommentsBeanX.HotCommentsBean sss : hotList) {
                        MComment mComment = new MComment();
                        mComment.setId(sss.getId());
                        mComment.setMovieId(s.getId());
                        mComment.setPeopleName(sss.getNick());
                        mComment.setPeopleImg(sss.getAvatarUrl());
                        mComment.setPeopleScore(sss.getScore() + "");
                        mComment.setPeopleTxt(sss.getContent() + "");
                        mComment.setPeopleTime(new Date(Long.parseLong(String.valueOf(sss.getTime()))));
                        mComment.setUpCount(sss.getUpCount());
                        mComment.setCreateTime(new Date());
                        mComment.setIsDelete("0");
                        commentService.insertOrUpdate(mComment);
                    }
                }
            }
        }
        movieService.updateBatchById(list);
        return "success";
    }

}
